const connect = require("../sql")

/**
 * @Description:  # TODO 获取所有班级信息
 * @Author: 文渊
 * @Date: 2023-08-29 15:43:51
 */
async function queryall(req,res){
    const sql = "select * from clazz"
    const arr = []
    const result = await connect(sql,arr)
    if(result.length){
        res.send({code:200,msg:result})
    }else{
        res.send({code:303,msg:"获取数据失败"})
    }
}

/**
 * @Description:  # TODO 根据班级ID删除班级信息
 * @Author: 文渊
 * @Date: 2023-08-30 10:30:59
 */
async function remove(req,res){
    const {id} = req.body;
    const sql = "delete from clazz where cid = ?"
    const result = await connect(sql,[id])
    if(result.affectedRows){
        res.send({code:200,msg:"删除班级成功"})
    }else{
        res.send({code:304,msg:"删除班级失败"})
    }
}

/**
 * @Description:  # TODO 添加班级信息
 * @Author: 文渊
 * @Date: 2023-08-30 14:16:12
 */
async function add(req,res){
    const {cname,count,subject} = req.body;
    const sql = "insert into clazz values(null,?,?,?)"
    const result = await connect(sql,[cname,count,subject])
    if(result.affectedRows){
        res.send({code:200,msg:"添加班级信息成功!"})
    }else{
        res.send({code:305,msg:"添加班级失败"})
    }
}


/**
 * @Description:  # TODO 根据ID获取班级信息
 * @Author: 文渊
 * @Date: 2023-08-30 14:43:03
 */ 
async function queryone(req,res){
    const {id} = req.query;
    const sql = "select * from clazz where cid = ?"
    const result = await connect(sql,[id])
    if(result.length){
        res.send({code:200,msg:result})
    }else{
        res.send({code:306,msg:"根据ID获取班级信息失败"})
    }
}

/**
 * @Description:  # TODO 修改班级信息
 * @Author: 文渊
 * @Date: 2023-08-30 14:46:10
 */
async function edit(req,res){
    const {id,cname,count,subject} = req.body;
    const sql = "update clazz set cname = ? , count = ? , subject = ? where cid = ?"
    const result = await connect(sql,[cname,count,subject,id])
    if(result.affectedRows){
        res.send({code:200,msg:"修改班级信息成功"})
    }else{
        res.send({code:307,msg:"修改班级信息失败"})
    }
}
module.exports = {
    queryall,remove,add,queryone,edit
};